/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import Modelo.DAL.Personal_DAO;
import Acceso.Security;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Rafael Barretta
 */
public class mostrarModificarPersonaAction extends org.apache.struts.action.Action {

    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    boolean validarCedula(ActionErrors errors, String cadena, String err, HttpServletRequest request, Pattern p) {
        Matcher m;
        m = p.matcher(cadena);
        if (!m.matches()) {
            errors.add(err, new ActionMessage("errors.invalid_Cedula"));
            saveErrors(request, errors);
            return true;
        }
        return false;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI", "Jefe Apoyo Administrativo", "Personal Administrativo"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación Mostrar Modificar");

        modificarPersonaForm formBean = (modificarPersonaForm) form;
        ActionErrors errors = new ActionErrors();

        boolean hayError = false;
        String voe = formBean.getVoe().trim();
        if (request.getParameter("voe").equals("0")) {
            voe = "V";
            formBean.setVoe("V");
        } else {
            voe = "E";
            formBean.setVoe("E");
        }
        String ci = request.getParameter("ced");

        Pattern p;

        // Validamos que la cedula no sea nula
        if (ci == null || ci.length() < 1) {
            errors.add("falta_Cedula", new ActionMessage("errors.falta_Cedula"));
            saveErrors(request, errors);
            hayError = true;
        }

        p = Pattern.compile("[0-9]*");
        hayError = (validarCedula(errors, ci, "invalid_Cedula", request, p) || hayError);

        /*/ Formateamos al numero de ceros necesarios para consistencia
        NumberFormat formatter = new DecimalFormat("##00000000");
        ci = "" + formatter.format(new Integer(ci));*/
        String CIcompuesta = voe + "-" + ci;
        System.out.println(CIcompuesta);


        if (!Personal_DAO.personalExiste(CIcompuesta)) {
            hayError = true;
            errors.add("persona_no_encontrada", new ActionMessage("errors.persona_no_encontrada"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        Object aux = Personal_DAO.llenarModForm(CIcompuesta, formBean);

        if (aux == null) {
            return mapping.findForward(FAILURE);
        }

        return mapping.findForward(SUCCESS);
    }
}
